﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DiracTCC.Business
{
    public class YuvAnalyser
    {
        private string path;
        private int width;
        private int height;
        private Chroma chroma;

        public YuvAnalyser(string path, int width, int height, Chroma chroma)
        {
            this.path = path;
            this.width = width;
            this.height = height;
            this.chroma = chroma;
        }

        public long GetNumberOfFrames()
        {
            long fileBytes = 0;
            double[] formatFactor = GetFormatFactor();

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                fileBytes = fs.Length;
            }

            // get from http://www.mathworks.co.uk/matlabcentral/fileexchange/11318
            return (long)Math.Ceiling(fileBytes / (width * height * (1 + 2 * formatFactor[0] * formatFactor[1])));
        }

        public double[] GetFormatFactor()
        {
            switch (chroma)
            {
                case Chroma.YUV444:
                    return new double[2] { 1, 1 };
                    break;
                case Chroma.YUV422:
                    return new double[2] { 0.5, 1 };
                    break;
                case Chroma.YUV420:
                    return new double[2] { 0.5, 0.5 };
                    break;
            }

            // error
            return new double[2] { 0, 0 };
        }
    }
}
